package com.hotel.po.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotel.po.mapper.ReviewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ReviewService {

    @Autowired
    private ReviewMapper reviewMapper;

    public PageInfo<Map<String, String>> list(Long hotelId, int page, int size) {
        PageHelper.startPage(page, size);
        List<Map<String, String>> data = reviewMapper.findByHotelId(hotelId);
        return new PageInfo<>(data);
    }

    public PageInfo<Map<String, String>> listAll(int page, int size) {
        PageHelper.startPage(page, size);
        List<Map<String, String>> data = reviewMapper.findAll();
        return new PageInfo<>(data);
    }

    public Long create(Long hotelId, Long userId, Integer rating, String content) {
        if (hotelId == null || userId == null || rating == null) {
            throw new IllegalArgumentException("hotelId/userId/rating 不能为空");
        }
        if (rating < 1 || rating > 5) {
            throw new IllegalArgumentException("rating 必须在 1-5 之间");
        }
        return reviewMapper.insert(hotelId, userId, rating, content);
    }

    public void delete(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("评价ID不能为空");
        }
        reviewMapper.delete(id);
    }
}


